using System;
using System.Data;
using System.Windows.Forms;
using ctsu.BaseForms;
using ctsu.Data;

namespace ctsu.Admin.Forms
{
	public class FormAncillaryAdmissionVisitSearch : System.Windows.Forms.Form
	{
		public VisitInfo SelectedVisitInfo;

		private Syncfusion.Windows.Forms.Tools.DateTimePickerAdv VisitDate;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label2;
		public System.Windows.Forms.ListView SearchResults;
		protected System.Windows.Forms.Button btnCancel;
		protected System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnRun;

		private SearchInfo _si;

		private System.ComponentModel.Container components = null;

		public FormAncillaryAdmissionVisitSearch(SearchInfo si, DateTime dt)
		{
			InitializeComponent();
			this._si = si;
			this.VisitDate.Value = dt;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormAncillaryAdmissionVisitSearch));
			this.VisitDate = new Syncfusion.Windows.Forms.Tools.DateTimePickerAdv();
			this.label4 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.SearchResults = new System.Windows.Forms.ListView();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnRun = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate.Calendar)).BeginInit();
			this.SuspendLayout();
			// 
			// VisitDate
			// 
			this.VisitDate.BackgroundColor = new Syncfusion.Drawing.BrushInfo();
			this.VisitDate.Border3DStyle = System.Windows.Forms.Border3DStyle.Flat;
			this.VisitDate.BorderColor = System.Drawing.SystemColors.ControlDark;
			this.VisitDate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			// 
			// 
			// 
			this.VisitDate.Calendar.AllowMultipleSelection = false;
			this.VisitDate.Calendar.Culture = new System.Globalization.CultureInfo("");
			this.VisitDate.Calendar.DaysFont = new System.Drawing.Font("Verdana", 8F);
			this.VisitDate.Calendar.DaysHeaderInterior = new Syncfusion.Drawing.BrushInfo(System.Drawing.SystemColors.Window);
			this.VisitDate.Calendar.Dock = System.Windows.Forms.DockStyle.Fill;
			this.VisitDate.Calendar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.VisitDate.Calendar.GridLines = Syncfusion.Windows.Forms.Grid.GridBorderStyle.None;
			this.VisitDate.Calendar.HeaderHeight = 20;
			this.VisitDate.Calendar.HeaderStartColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(128)))), ((int)(((byte)(62)))));
			this.VisitDate.Calendar.Location = new System.Drawing.Point(0, 0);
			this.VisitDate.Calendar.MinValue = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
			this.VisitDate.Calendar.Name = "monthCalendar";
			// 
			// 
			// 
			this.VisitDate.Calendar.NoneButton.Location = new System.Drawing.Point(131, 0);
			this.VisitDate.Calendar.ScrollButtonSize = new System.Drawing.Size(24, 24);
			this.VisitDate.Calendar.SelectedDates = new System.DateTime[] {
        new System.DateTime(2007, 4, 5, 6, 50, 27, 49)};
			this.VisitDate.Calendar.Size = new System.Drawing.Size(203, 174);
			this.VisitDate.Calendar.SizeToFit = true;
			this.VisitDate.Calendar.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
			this.VisitDate.Calendar.TabIndex = 0;
			// 
			// 
			// 
			this.VisitDate.Calendar.TodayButton.Location = new System.Drawing.Point(0, 0);
			this.VisitDate.Calendar.TodayButton.Size = new System.Drawing.Size(131, 20);
			this.VisitDate.Calendar.WeekFont = new System.Drawing.Font("Verdana", 8F);
			this.VisitDate.Calendar.WeekInterior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Vertical, System.Drawing.Color.PeachPuff, System.Drawing.Color.AntiqueWhite);
			this.VisitDate.DropDownImage = null;
			this.VisitDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
			this.VisitDate.Location = new System.Drawing.Point(64, 8);
			this.VisitDate.Name = "VisitDate";
			this.VisitDate.NullString = "";
			this.VisitDate.ShowCheckBox = false;
			this.VisitDate.Size = new System.Drawing.Size(88, 20);
			this.VisitDate.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
			this.VisitDate.TabIndex = 133;
			this.VisitDate.Value = new System.DateTime(2007, 4, 5, 6, 50, 27, 49);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 8);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 132;
			this.label4.Text = "Visit Date:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 135;
			this.label2.Text = "Search results:";
			// 
			// SearchResults
			// 
			this.SearchResults.Activation = System.Windows.Forms.ItemActivation.TwoClick;
			this.SearchResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.SearchResults.FullRowSelect = true;
			this.SearchResults.GridLines = true;
			this.SearchResults.HideSelection = false;
			this.SearchResults.Location = new System.Drawing.Point(8, 48);
			this.SearchResults.MultiSelect = false;
			this.SearchResults.Name = "SearchResults";
			this.SearchResults.Size = new System.Drawing.Size(624, 176);
			this.SearchResults.TabIndex = 0;
			this.SearchResults.UseCompatibleStateImageBehavior = false;
			this.SearchResults.View = System.Windows.Forms.View.Details;
			this.SearchResults.ItemActivate += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnCancel.Image = ((System.Drawing.Image)(resources.GetObject("btnCancel.Image")));
			this.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnCancel.Location = new System.Drawing.Point(320, 232);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(80, 23);
			this.btnCancel.TabIndex = 137;
			this.btnCancel.Text = "&Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnOK.Image = ((System.Drawing.Image)(resources.GetObject("btnOK.Image")));
			this.btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnOK.Location = new System.Drawing.Point(224, 232);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(80, 23);
			this.btnOK.TabIndex = 136;
			this.btnOK.Text = "&OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnRun
			// 
			this.btnRun.Location = new System.Drawing.Point(160, 8);
			this.btnRun.Name = "btnRun";
			this.btnRun.Size = new System.Drawing.Size(75, 23);
			this.btnRun.TabIndex = 138;
			this.btnRun.Text = "Run Search";
			this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
			// 
			// FormAncillaryAdmissionVisitSearch
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(640, 262);
			this.Controls.Add(this.btnRun);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.SearchResults);
			this.Controls.Add(this.VisitDate);
			this.Controls.Add(this.label4);
			this.Name = "FormAncillaryAdmissionVisitSearch";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Ancillary - Admission Visit Search";
			this.Load += new System.EventHandler(this.FormAncillaryAdmissionVisitSearch_Load);
			((System.ComponentModel.ISupportInitialize)(this.VisitDate.Calendar)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void FormAncillaryAdmissionVisitSearch_Load(object sender, System.EventArgs e)
		{
			InitializeResultCols();
			RunSearch();

		}

		private void InitializeResultCols()
		{
			//design list view for result fields						
			SearchResults.Columns.Clear();

			//			DataColumn dc;
			//			string fieldType;				
			foreach (ReportColumn rc in _si.SearchResultCols)
			{
				ColumnHeader ch = new ColumnHeader();
				ch.Text = rc.DisplayName;

				//get width from schema
				//				dc = _si.TableSchema.Columns[src.FieldName];
				//				fieldType = dc.DataType.ToString();

				//auto size column based on display text
				ch.Width = -2;
				SearchResults.Columns.Add(ch);
			}

		}

		private void RunSearch()
		{
			Cursor.Current = Cursors.WaitCursor;
			SearchResults.Items.Clear();
			string sql = this._si.SqlCommand;

			if (this._si.TableName == "AncillaryAdmissionVisit")
				sql += " AND p.VisitDate <= '" + this.VisitDate.Value.ToShortDateString() + "'" +
					" AND p.DischargeDate >= '" + this.VisitDate.Value.ToShortDateString() + "'";
			else
				sql += " AND p.VisitDate = '" + this.VisitDate.Value.ToShortDateString() + "'";

			//SqlConnection conn = LoginForm.GetSqlConnection();
			IDataReader reader = null;
			try
			{
				reader = DataAccess.GetDataReader(sql);

				while (reader.Read())
				{
					ListViewItem lvi = new ListViewItem();
					int i = 0;
					string data;
					foreach (ReportColumn rc in _si.SearchResultCols)
					{
						if (reader.IsDBNull(i))
							data = "";
						else
							data = reader[rc.FieldName].ToString();
						if (i == 0)
							lvi.Text = data;
						else
							lvi.SubItems.Add(data);
						i++;
					}
					SearchResults.Items.Add(lvi);
				}

				//resize the listview columns according to the results
				foreach (ColumnHeader ch in SearchResults.Columns)
					ch.Width = -2;

			}
			catch
			{
			}
			finally
			{
				if (reader != null)
				{
					if (!reader.IsClosed)
						reader.Close();
				}
				//if(conn != null)
				//    conn.Dispose();
				SearchResults.EndUpdate();
			}
		}

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			RunSearch();
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if (SearchResults.Items.Count > 0)
			{
				if (SearchResults.SelectedItems.Count == 0)
				{
					this.SelectedVisitInfo = null;
					MessageBox.Show("A row must be selected when you press the OK button");
					//this.DialogResult = DialogResult.No;
					return;
				}
				//this depends on the first column (id) being an int
				ListViewItem lvi = SearchResults.SelectedItems[0];
				this.SelectedVisitInfo = new VisitInfo();
				VisitInfo vi = this.SelectedVisitInfo;
				vi.VisitDate = this.VisitDate.Value;
				vi.ID = Convert.ToInt32(lvi.Text);
				vi.PatientID = Convert.ToInt32(lvi.SubItems[1].Text);
				vi.ProtocolID = Convert.ToInt32(lvi.SubItems[4].Text);
				//vi.SPID = Convert.ToInt32(lvi.SubItems[5].Text);
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			else
			{
				this.SelectedVisitInfo = null;
				this.DialogResult = DialogResult.No;
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.SelectedVisitInfo = null;
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

	}
}
